/*******************************************************************************
 *
 * Copyright (c) 2008-2011 ayound@gmail.com, guwei8576431@gmail.com
 * This program and the accompanying materials
 * are made available under the terms of the GNU GENERAL PUBLIC LICENSE
 * which accompanies this distribution, and is available at
 * http://dev.perl.org/licenses/gpl1.html
 * All rights reserved.
 *
 * Created on 2009-10-22
 *******************************************************************************/

package org.cgtw.scriptprofile.server.servlet.processor;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import org.cgtw.scriptprofile.engine.manager.CharsetDetector;
import org.cgtw.scriptprofile.server.manager.ResourceManager;
import org.cgtw.scriptprofile.server.manager.ScriptProfileServerManager;
import org.cgtw.scriptprofile.server.servlet.ScriptProfileRequest;
import org.cgtw.scriptprofile.server.servlet.ScriptProfileResponse;
import org.cgtw.scriptprofile.server.servlet.ScriptProfileResponseInfo;

public class HtmlProcessor extends AbstractProcessor {

	public HtmlProcessor(ScriptProfileRequest request,
			ScriptProfileResponse response, ScriptProfileResponseInfo info) {
		super(request, response, info);
	}

	@Override
	public void process() {
		try {
			URL url = ProcessorFactory.getRemoteURL(getRequest()
					.getRequestSource());
			String resourcePath = url.getPath();
			String realPath = ResourceManager.getInstance().createResourceFile(
					resourcePath, getInfo());
			String encoding = getInfo().getEncoding();
			if (encoding == null || "gzip".equals(encoding)) {
				CharsetDetector detector = new CharsetDetector();
				detector.detect(realPath);
				encoding = detector.getCharset();
			}

			getResponse().writeHTMLHeader(encoding,
					getInfo().getResponseHeader());
			// write debug javascript file before any one
			String line = null;
			StringBuffer buffer = new StringBuffer();
			InputStream homeStream = new FileInputStream(realPath);
			BufferedReader homeInputStream = new BufferedReader(
					new InputStreamReader(homeStream, encoding));
			line = null;
			while ((line = homeInputStream.readLine()) != null) {
				buffer.append(line).append("\n");
			}
			String scriptContent = buffer.toString();

			// compile html file by javascript engine

			String htmlResult = ScriptProfileServerManager.getInstance().getServer()
					.getCompileEngine().compileScript(realPath, scriptContent);

			getResponse().write(htmlResult);

			if (homeStream != null) {
				homeStream.close();
			}

			if (homeInputStream != null) {
				homeInputStream.close();
			}

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			getResponse().close();
		}

	}

}
